<template>
  <div>
    <h1>分类列表</h1>
    <div style="margin-bottom: 10px">
      <el-button type="primary" @click="onAddShow">新增商品类型</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="name" label="名称" sortable width="180px" />
      <el-table-column prop="typeImage" label="图片" sortable width="120px">
        <template #default="scope">
          <el-image
            style="width: 50px; height: 50px"
            v-if="scope.row.typeImage"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column prop="typeStatus" label="状态" sortable width="120px">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.typeStatus == 0">显示</el-tag>
          <el-tag type="info" v-if="scope.row.typeStatus == 1">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="排序" sortable width="120px" />
      <el-table-column label="操作" sortable>
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button type="danger" size="small" @click="handleAdd(scope.row)"
            >添加</el-button
          >
          <el-button
            :type="scope.row.typeStatus == 0 ? 'info' : 'success'"
            size="small"
            @click="handleShowAndHide(scope.row)"
            >{{ scope.row.typeStatus == 0 ? "隐藏" : "显示" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加---对话框 -->
    <div>
      <el-dialog v-model="AddFormVisible" :title="title" width="500">
        <el-form :model="form">
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="分类图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/prod/type/loadImage"
              :show-file-list="false"
              :headers="headers"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="form.typeTop" autocomplete="off" />
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0">显示</el-radio>
              <el-radio :value="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeAdd">取消</el-button>
            <el-button
              v-if="title == '添加商品类型'"
              type="primary"
              @click="onSubmitAdd"
            >
              提交
            </el-button>
            <el-button
              v-if="title != '添加商品类型'"
              type="primary"
              @click="onSubmitEdit"
            >
              保存
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import {
  getTypeTreeList,
  showAndHide,
  deleteByTypeId,
  addType,
  updateType,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
import { RefSymbol } from "@vue/reactivity";

const tableData = ref([]);

const AddFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  typeName: "",
  typeImage: "",
  typeTop: "",
  typeStatus: 0,
  typePid: 0,
});

const imageUrl = ref("");

const title = ref("添加商品类型");

const handleEdit = (row: any) => {
  console.log("handleEdit", row.id, row.typeImage);
  title.value = "编辑商品类型信息";
  row.typeId = row.id;
  form.value.typeName = row.name;
  form.value.typePid = row.parentId;
  imageUrl.value = row.typeImage;
  Object.assign(form.value, row);
  AddFormVisible.value = true;
};

const onSubmitEdit = () => {
  form.value.typeImage = imageUrl.value;
  updateType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadTypeList();
      closeAdd();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const handleAdd = (row: any) => {
  title.value = "添加商品类型";
  console.log("handleAdd", row.id);
  form.value.typePid = row.id;
  AddFormVisible.value = true;
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  imageUrl.value = response.data;
};

const headers = ref({
  token: sessionStorage.getItem("token"),
});

const onAddShow = () => {
  console.log("onAddShow");
  title.value = "添加商品类型";
  AddFormVisible.value = true;
};

const onSubmitAdd = () => {
  form.value.typeImage = imageUrl.value;
  addType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadTypeList();
      closeAdd();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const closeAdd = () => {
  form.value = {
    typeName: "",
    typeImage: "",
    typeTop: "",
    typeStatus: 0,
    typePid: 0,
  };
  imageUrl.value = "";
  AddFormVisible.value = false;
};

const handleDelete = (row: any) => {
  console.log("handleDelete", row.id);
  row.typeId = row.id;
  ElMessageBox.confirm("确认删除此类别吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteByTypeId(row.typeId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadTypeList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

const handleShowAndHide = (row: any) => {
  console.log("handleEdit", row.id, row.typeStatus);
  row.typeId = row.id;
  showAndHide(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("操作完成");
      loadTypeList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const loadTypeList = () => {
  getTypeTreeList().then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
    }
  });
};

onMounted(() => {
  loadTypeList();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
